import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/gt_description_v3.dart';

enum GTCollapseTypeV3 { defaultType, card }

class GTCollapseV3 extends StatefulWidget {
  final List<GTDescriptionModalV3>? list;
  final String? title;
  final GTCollapseTypeV3? type;
  final String? descriptionTitle;
  final bool? isVisible;
  final ValueChanged<bool>? onClick;
  final String? content;

  const GTCollapseV3({
    super.key,
    this.list,
    this.title,
    this.type,
    this.descriptionTitle,
    this.isVisible,
    this.onClick,
    this.content,
  });

  @override
  State<StatefulWidget> createState() => _CollapseStateV3();
}

class _CollapseStateV3 extends State<GTCollapseV3> {
  late Color backgroundColor;
  late double paddingHorizontal, radius;
  late bool isVisible;

  @override
  void initState() {
    isVisible = widget.isVisible ?? false;
    switch (widget.type ?? GTCollapseTypeV3.defaultType) {
      case GTCollapseTypeV3.defaultType:
        backgroundColor = GTColorsV3.bg1.color;
        paddingHorizontal = 24;
        radius = 0;
        break;
      case GTCollapseTypeV3.card:
        backgroundColor = GTColorsV3.card1.color;
        paddingHorizontal = 12;
        radius = 6;
        break;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GestureDetector(
          onTap: () {
            setState(() {
              isVisible = !isVisible;
              widget.onClick?.call(isVisible);
            });
          },
          child: Container(
            decoration: BoxDecoration(color: backgroundColor, borderRadius: BorderRadius.circular(radius)),
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.symmetric(horizontal: paddingHorizontal),
                  height: 40,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        widget.title ?? "",
                        style: gtTextStyleW500(color: GTColorsV3.text1.color, fontSize: 14),
                      ),
                      const SizedBox(
                        width: 12,
                      ),
                      Icon(
                        isVisible ? GTIconFont.e8c9 : GTIconFont.e8c8,
                        size: 16,
                        color: isVisible ? GTColorsV3.icon9.color : GTColorsV3.icon1.color,
                      ),
                    ],
                  ),
                ),
                if (widget.list != null) ...[
                  Visibility(
                      visible: isVisible,
                      child:
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: widget.type == GTCollapseTypeV3.card ? 0 : 12),
                        child: ClipRRect(
                          borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(radius),
                            bottomRight: Radius.circular(radius),
                          ),
                          child: Column(
                            children: [
                              if (widget.type == GTCollapseTypeV3.card) ...[
                                Container(
                                  height: 1,
                                  color: GTColorsV3.divider2.color,
                                ),
                              ],
                              GTDescriptionV3(
                                list: widget.list ?? [],
                                title: widget.descriptionTitle,
                                smallStyle: true,
                              ),
                            ],
                          ),
                        ),

                      ))
                ],
                if (widget.content != null) ...[
                  if ((widget.type ?? GTCollapseTypeV3.defaultType) == GTCollapseTypeV3.card) ...[
                    Visibility(
                      visible: isVisible,
                      child: Column(
                        children: [
                          Container(
                            height: 1,
                            color: GTColorsV3.divider2.color,
                          ),
                          Container(
                            padding: const EdgeInsets.all(12),
                            child: Text(
                              widget.content ?? "",
                              style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text3.color),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ] else ...[
                    Visibility(
                      visible: isVisible,
                      child: Container(
                        color: backgroundColor,
                        padding: const EdgeInsets.symmetric(horizontal: 12),
                        child: Container(
                          decoration: BoxDecoration(color: GTColorsV3.card1.color),
                          padding: const EdgeInsets.all(12),
                          child: Text(
                            widget.content ?? "",
                            style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text3.color),
                          ),
                        ),
                      ),
                    ),
                  ]
                ],
              ],
            ),
          ),
        ),
      ],
    );
  }

  @override
  void didUpdateWidget(covariant GTCollapseV3 oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.isVisible != null) {
      isVisible = widget.isVisible ?? false;
    }
  }
}
